//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "LinearRegression.h"
using namespace Cephei::QL::Math;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Math/GeneralLinearLeastSquares.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::CLinearRegression::CLinearRegression (boost::shared_ptr<QuantLib::LinearRegression>& childNative, Object^ owner) : CGeneralLinearLeastSquares(CLinearRegression::typeid)
{
#ifdef HANDLE
	_phLinearRegression = NULL;
#endif
	_ppLinearRegression = &childNative;
    _ppGeneralLinearLeastSquares = new boost::shared_ptr<QuantLib::GeneralLinearLeastSquares> (boost::dynamic_pointer_cast<QuantLib::GeneralLinearLeastSquares> (*_ppLinearRegression));
}
Cephei::QL::Math::CLinearRegression::CLinearRegression (QuantLib::LinearRegression& childNative, Object^ owner) : CGeneralLinearLeastSquares(CLinearRegression::typeid)
{
#ifdef HANDLE
	_phLinearRegression = NULL;
#endif
	_ppLinearRegression = new boost::shared_ptr<QuantLib::LinearRegression> (&childNative);
    _ppGeneralLinearLeastSquares = new boost::shared_ptr<QuantLib::GeneralLinearLeastSquares> (boost::dynamic_pointer_cast<QuantLib::GeneralLinearLeastSquares> (*_ppLinearRegression));
    _LinearRegressionOwner = owner;
    _GeneralLinearLeastSquaresOwner = owner;
}

Cephei::QL::Math::CLinearRegression::CLinearRegression (CLinearRegression^ copy) : CGeneralLinearLeastSquares(CLinearRegression::typeid)
{
#ifdef HANDLE
	_phLinearRegression = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLinearRegression = new boost::shared_ptr<QuantLib::LinearRegression> (copy->GetShared());
        _ppGeneralLinearLeastSquares = new boost::shared_ptr<QuantLib::GeneralLinearLeastSquares> (boost::dynamic_pointer_cast<QuantLib::GeneralLinearLeastSquares> (*_ppLinearRegression));
    }
}
Cephei::QL::Math::CLinearRegression::CLinearRegression (PLATFORM::Type^ t) : CGeneralLinearLeastSquares(CLinearRegression::typeid)
{
#ifdef HANDLE
	_phLinearRegression = NULL;
#endif
	if (!t->IsSubclassOf(CLinearRegression::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::CLinearRegression::CLinearRegression (QuantLib::Handle<QuantLib::LinearRegression>& childNative, Object^ owner)  : CGeneralLinearLeastSquares(CLinearRegression::typeid)
{
	_phLinearRegression = &childNative;
	_ppLinearRegression = &static_cast<boost::shared_ptr<QuantLib::LinearRegression>>(childNative.currentLink());
    _ppGeneralLinearLeastSquares = new boost::shared_ptr<QuantLib::GeneralLinearLeastSquares> (boost::dynamic_pointer_cast<QuantLib::GeneralLinearLeastSquares> (*_ppLinearRegression));
    _LinearRegressionOwner = owner;
}
Cephei::QL::Math::CLinearRegression::CLinearRegression (QuantLib::Handle<QuantLib::LinearRegression> childNative)  : CGeneralLinearLeastSquares(CLinearRegression::typeid)
{
	_phLinearRegression = &childNative;
	_ppLinearRegression = &static_cast<boost::shared_ptr<QuantLib::LinearRegression>>(childNative.currentLink());
    _ppGeneralLinearLeastSquares = new boost::shared_ptr<QuantLib::GeneralLinearLeastSquares> (boost::dynamic_pointer_cast<QuantLib::GeneralLinearLeastSquares> (*_ppLinearRegression));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::CLinearRegression::CLinearRegression (QuantLib::LinearRegression childNative)  : CGeneralLinearLeastSquares(CLinearRegression::typeid)
{
#ifdef HANDLE
	_phLinearRegression = NULL;
#endif
	_ppLinearRegression = new boost::shared_ptr<QuantLib::LinearRegression> (new QuantLib::LinearRegression (childNative));
    _ppGeneralLinearLeastSquares = new boost::shared_ptr<QuantLib::GeneralLinearLeastSquares> (boost::dynamic_pointer_cast<QuantLib::GeneralLinearLeastSquares> (*_ppLinearRegression));
}
#endif

Cephei::QL::Math::CLinearRegression::~CLinearRegression ()
{
    if (_ppLinearRegression != NULL)
    {
	    delete _ppLinearRegression;
        _ppLinearRegression = NULL;
    }
}
Cephei::QL::Math::CLinearRegression::!CLinearRegression ()
{
    if (_ppLinearRegression != NULL)
    {
	    delete _ppLinearRegression;
    }
}
QuantLib::LinearRegression& Cephei::QL::Math::CLinearRegression::GetReference ()
{
    if (_ppLinearRegression == NULL) throw REFNEW NativeNullException ();
	return **_ppLinearRegression;
}
boost::shared_ptr<QuantLib::LinearRegression>& Cephei::QL::Math::CLinearRegression::GetShared ()
{
    if (_ppLinearRegression == NULL) throw REFNEW NativeNullException ();
	return *_ppLinearRegression;
}
QuantLib::LinearRegression* Cephei::QL::Math::CLinearRegression::GetPointer ()
{
    if (_ppLinearRegression == NULL) throw REFNEW NativeNullException ();
	return &**_ppLinearRegression;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LinearRegression>& Cephei::QL::Math::CLinearRegression::GetHandle ()
{
	if (_phLinearRegression == NULL)
	{
		_phLinearRegression = new Handle<QuantLib::LinearRegression> (*_ppLinearRegression);
	}
	return *_phLinearRegression;
}
#endif
bool Cephei::QL::Math::CLinearRegression::HasNative () 
{
	return (_ppLinearRegression != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

